package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/order")
public class OrderMobileController {
    @Reference
    OrderService orderService;

    @Autowired
    JedisPool jedisPool;
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map<String,Object> map) throws Exception {
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        String code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        if(code==null&&!code.equals(validateCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        Result result=orderService.submit(map);
        return result;
    }

    @RequestMapping("/findById")
    public Result findById(String id) throws Exception {
       Map<String,Object>map = orderService.findById(id);
       return new Result(true,"",map);
    }

}
